package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Reference
    RoleService roleService;

//    //新增
//    @RequestMapping("/add")
//    public Result add(@RequestBody Role role, Integer[] permissionIds){
//        log.debug("RoleController-add: Role{ " + role + " }");
//        log.debug("RoleController-add: permissionIds{ " + Arrays.toString(permissionIds) + " }");
//        roleService.add(role,permissionIds);
//        if(role.getName()==null){
//            return new Result(false,"角色名称不能为空");
//        }
//        if(permissionIds==null){
//            return new Result(false,"角色权限不能为空");
//        }
//        log.debug("添加角色成功");
//        return new Result(true,"添加角色成功");
//    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds){
        log.debug("RoleController-add: Role{ " + role + " }");
        log.debug("RoleController-add: permissionIds{ " + Arrays.toString(permissionIds) + " }");
        log.debug("RoleController-add: menuIds{ " + Arrays.toString(menuIds) + " }");
        if(role.getName().length()==0){
            return new Result(false,"角色名称不能为空");
        }
        if(permissionIds.length==0){
            return new Result(false,"角色权限不能为空");
        }
         if(menuIds.length==0){
            return new Result(false,"角色展示的菜单不能为空");
        }
//        if(role.getName()!=null){
//            return new Result(false,"角色名称不能重复");
//        }
        roleService.add(role,permissionIds,menuIds);
        log.debug("添加角色成功");
        return new Result(true,"添加角色成功");
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        log.debug("RoleController-findPage: QueryPageBean{ " + pageBean + " }");
        PageResult pageResult = roleService.findPage(pageBean);
        log.debug("RoleController-findPage: PageResult {" + pageResult + " }");
        return pageResult;
    }

    //删除
    @RequestMapping("/delById")
    public Result delById(Integer id){
        log.debug("delById : { " + id +"}" );
        roleService.delById(id);
        log.debug("删除角色成功");
        return new Result(true,"删除角色成功");
    }

    //根据id查询角色
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("RoleController-findById: id{ " + id + " }");
        Role role = roleService.findById(id);
        log.debug("RoleController-findById: RoleGroup{ " + role + " }");
        return new Result(true, "查询角色成功",role);
    }

    //根据角色id查询关联的权限id
    @RequestMapping("/findpermissionIdsByroleId")
    public Result findpermissionIdsByroleId(Integer roleId){
        log.debug("RoleController-findpermissionIdsByroleId: roleId: { " + roleId + " }");
        List<Integer> list = roleService.findpermissionIdsByroleId(roleId);
        log.debug("permissionIds: { " + list + " }");
        return new Result(true,"查询权限成功",list);
    }
    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        log.debug("RoleController-edit: role{ " + role + " }");
        log.debug("RoleController-edit: permissionIds{ " + Arrays.toString(permissionIds) + " }");
        if(role.getName().length()==0){
            return new Result(false,"角色名称不能为空");
        }
        if(permissionIds.length==0){
            return new Result(false,"角色权限不能为空");
        }
        if(menuIds.length==0){
            return new Result(false,"角色展示的菜单不能为空");
        }
        roleService.edit(role,permissionIds,menuIds);
        log.debug("修改角色成功");
        return new Result(true,"修改角色成功");
    }

    //根据角色id查询关联的菜单id
    @RequestMapping("/findmenuIdsByroleId")
    public Result findmenuIdsByroleId(Integer roleId){
        log.debug("RoleController-findmenuIdsByroleId: roleId: { " + roleId + " }");
        List<Integer> list = roleService.findmenuIdsByroleId(roleId);
        log.debug("permissionIds: { " + list + " }");
        return new Result(true,"查询菜单成功",list);
    }

}
